//
//  CYGFoundViewController.m
//  CYGHuntForCity
//
//  Created by cygarm on 16/2/20.
//  Copyright © 2016年 cygarm. All rights reserved.
//

#import "CYGFoundViewController.h"
#import "UIViewController+CYGLeftMenuChild.h"
#import "CYGFoundHeaderView.h"
#import "CYGFoundCell.h"
#import "CYGFoundModel.h"
#import "CYGHotsViewController.h"

@interface CYGFoundViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong, nonatomic) NSMutableArray<CYGFoundModel *> *datas;
@end

@implementation CYGFoundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
	[self setupUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSArray *)datas {
	if (_datas == nil) {
		NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"foundData" ofType:@"plist"]];
		_datas = [CYGFoundModel mj_objectArrayWithKeyValuesArray:dictArray];
	}
	
	return _datas;
}

#pragma mark - UI Setup
- (void)setupUI {
	self.navigationItem.title = @"发现";
	
	self.collectionView.backgroundColor = [UIColor whiteColor];
	// 由于使用了nib替代了storyboard中的cell,这里需要另行注册
	[self.collectionView registerNib:[UINib nibWithNibName:@"CYGFoundCell" bundle:nil] forCellWithReuseIdentifier:@"idFoundCell"];
	
	UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
	CGFloat itemW = 100.0;
	CGFloat itemH = 135.0;
    // cell的大小
    layout.itemSize = CGSizeMake(itemW, itemH);
    //最小左右间距
    layout.minimumInteritemSpacing = 0;
    //最小上下间距
    layout.minimumLineSpacing = 20;
    //每个headView的大小
    layout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 50);
	layout.footerReferenceSize = CGSizeMake(self.view.bounds.size.width, 2);
    //内边距
    CGFloat marginLR = (self.view.bounds.size.width - 3*itemW)/4;
    layout.sectionInset = UIEdgeInsetsMake(20, marginLR, 20, marginLR);
	[self.collectionView setCollectionViewLayout:layout];
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
	DLog(@"%@", segue.description);
}

#pragma mark - UICollectionViewController delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
	return self.datas.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
	return [self.datas[section].cells count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
	CYGFoundCellModel *model = self.datas[indexPath.section].cells[indexPath.row];
	CYGFoundCell *cell = [CYGFoundCell cellWithCollectionView:collectionView forIndexPath:indexPath ofModel:model];
	
	return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
	UICollectionReusableView *view;
	if (kind == UICollectionElementKindSectionHeader) {
		CYGFoundHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"idFoundHeader" forIndexPath:indexPath];
		headerView.headTitle.text = self.datas[indexPath.section].header;
		view = headerView;
	}
	
	if (kind == UICollectionElementKindSectionFooter) {
		UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"idFoundFooter" forIndexPath:indexPath];
		view = footerView;
	}
	
	return view;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
	CYGFoundCellModel *cell = self.datas[indexPath.section].cells[indexPath.row];
	
	CYGHotsViewController *hotsVC = [[CYGHotsViewController alloc] init];
	hotsVC.barTitle = cell.title;
	[self.navigationController pushViewController:hotsVC animated:YES];
}

@end
